﻿using System.Web.UI;


namespace BigfootDNN.Mvc
{

    /// <summary>
    /// This is the class from which all mvc views need to inherit. It provides
    /// a series of methods and classes that give you easy access to the different helpers throughout the system
    /// It also provides aliased functions like RenderPartial which is part of the JSHelper for easy access.
    /// </summary>
    public class MvcBaseView : UserControl
    {

        /// <summary>
        /// MVC Request Route
        /// </summary>
        public RouteInfo Route { get; set; } 

        /// <summary>
        /// THIS FIELD IS SET BY THE ROUTE.RENDERPARTIAL TO INDICATE TO THE BASE VIEW THAT IT IS BEING RENDERED BY IT. 
        /// </summary>
        public bool IsRenderPartial;

        /// <summary>
        /// Inclues all view helper functions (i.e. RenderPartial, E, UrlP, Url, Html, JS, TB, Ajax, CreatePartialForm, SubmitPartialForm, RenderIf, Post values, etc.)
        /// </summary>
        public DnnViewHelper Helper
        {
            get { return _viewHelper ?? (_viewHelper = new DnnViewHelper(Route, IsRenderPartial)); }
        }
        private DnnViewHelper _viewHelper;
        
        /// <summary>
        /// Alias to Helper
        /// </summary>
        public DnnViewHelper BF
        {
            get
            {
                return Helper;
            }
        }
        

        ///// <summary>
        ///// Render a partial view onto the page
        ///// </summary>
        ///// <param name="ViewName">The name of the view within the current controller to render</param>
        ///// <returns>The rendered html output of the view</returns>
        //public string RenderPartial(string ViewName)
        //{
        //    return RenderPartial(Route.Controller, ViewName, null);
        //}

        ///// <summary>
        ///// Render a partial view onto the page
        ///// </summary>
        ///// <param name="ViewName">The name of the view within the current controller to render</param>
        ///// <param name="data">The data to be used as the views model</param>
        ///// <returns>The rendered html output of the view</returns>
        //public string RenderPartial(string ViewName, object data)
        //{
        //    return RenderPartial(Route.Controller, ViewName, data);
        //}

        ///// <summary>
        ///// Render a partial view onto the page only if the specified condition is met
        ///// </summary>
        ///// <param name="Condition">The condition that must be met in order for the view to be rendered</param>
        ///// <param name="ViewName">The name of the view within the current controller to render</param>
        ///// <param name="data">The data to be used as the views model</param>
        ///// <returns>The rendered html output of the view</returns>
        //public string RenderPartialIf(bool Condition, string ViewName, object data)
        //{
        //    return Condition ? RenderPartial(ViewName, data) : "";
        //}

        ///// <summary>
        ///// Render a partial view onto the page only if the specified condition is met
        ///// </summary>
        ///// <param name="Condition">The condition that must be met in order for the view to be rendered</param>
        ///// <param name="ControllerName">The name of the controller folder where the view exists</param>
        ///// <param name="ViewName">The name of the view within the controller folder specified</param>
        ///// <param name="data">The data to be used as the views model</param>
        ///// <returns>The rendered html output of the view</returns>
        //public string RenderPartialIf(bool Condition, string ControllerName, string ViewName, object data)
        //{
        //    return Condition ? RenderPartial(ControllerName, ViewName, data) : "";
        //}

        ///// <summary>
        ///// Render a partial view onto the page.
        ///// </summary>
        ///// <param name="ControllerName">The name of the controller folder where the view exists</param>
        ///// <param name="ViewName">The name of the view within the controller folder</param>
        ///// <param name="data">The data to be used as the view model</param>
        ///// <returns>The rendered html output of the view</returns>
        //public string RenderPartial(string ControllerName, string ViewName, object data)
        //{
        //    try
        //    {
        //        return RenderPartial(Route.App.Server, RouteHelper.ControlPath(ControllerName, ViewName, Route.App), data, "Route", Route);
        //    }
        //    catch (Exception ex)
        //    {
        //        Route.App.HandleException(ex, false);
        //        return RenderPartial(Route.App.Server, Route.App.ErrorViewPath, ErrorViewModel.Create(ex, Route.App.Request.Url.ToString()));
        //    }
        //}

        ///// <summary>
        ///// This functions is called from the markup to handle any view error. It has the logic to know weather to output the error to the screen
        ///// or in the case of being partially rendered to raise the error to the RenderView function
        ///// </summary>
        ///// <param name="ex">The exception to be handled</param>
        //public void HandleViewError(Exception ex)
        //{
        //    if (ex == null) throw new ArgumentNullException("ex");
        //    // If it is rendering in partial mode, then render the error.
        //    if (IsRenderPartial) throw ex;

        //    Route.App.HandleException(ex, false);
        //    Route.App.Response.Write(RenderPartial("shared", "error", ErrorViewModel.Create(ex, Route.App.Request.Url.ToString())));
        //}

        //private UrlHelper _url;
        ///// <summary>
        ///// Creates a new instance of the UrlHelper class to help you create urls
        ///// </summary>
        //public UrlHelper Url  {
        //    get { return _url ?? (_url = new UrlHelper(Route)); }
        //}

        ///// <summary>
        ///// Creates a unique client id specific to this module instance on the page. This is useful when refering to element ids from javascript as
        ///// you might have the same id if the module is added to the page several times. It simply adds an underscore plus the 
        ///// module id to the name
        ///// </summary>
        ///// <param name="id">The id of the element you would like to make unique to the module</param>
        ///// <returns>It returns {id}_{ModuleId}</returns>
        //public string uid(string id)
        //{
        //    return id + "_" + Route.ModData.ModuleID;
        //}

        ///// <summary>
        ///// Class used to aid in the collection of posted values from the request. First it tries the QueryString and second it tries the form collection
        ///// </summary>
        //public new PostHelper Values
        //{
        //   get
        //   {
        //       return Route.App.Values;  
        //   } 
        //}
    }
}

